//
//  XDLMSanPostTwoTableViewCell.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/2/20.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDLMSanPostTwoTableViewCell.h"
#import "XDLMSanPostFiveTableViewCell.h"
#define TCOLOR [UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:115.0/255.0 alpha:1]
#define TEXTCOLOR [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1]
@implementation XDLMSanPostTwoTableViewCell
{
    UILabel *_titleLab;
    UIImageView *_imgView;
    UIView *_newbgView;
    UITableView *_table;
    NSArray *imageArray;
//    NSArray *titleArray;
    NSArray *textArray;
}

- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        imageArray = @[@"repay",@"",@""];
//        titleArray = @[@"借款用途",@"还款来源",@"风控措施"];
        _titleLab = [self createLabelWithFrame:CGRectMake(15, 10, 100*kscale, 30*kscale) text:@"" textFont:[UIFont systemFontOfSize:16] textColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1]];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW - 15- 9*kscale, 17*kscale, 6, 10)];
        _btn.selected = NO;
        [_btn addTarget:self action:@selector(btnCilck:) forControlEvents:UIControlEventTouchUpInside];
        [_btn setBackgroundImage:[UIImage imageNamed:@"rightcell"] forState:UIControlStateNormal];
        [self addSubview:_imgView];
        [self addSubview:_titleLab];
        [self addSubview:_btn];
    }
    return self;
}
- (void)setUI
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, self.frame.size.height)];
        [self.contentView addSubview:_bgView];
        [_bgView setHidden:YES];
       
        float sum = 0;
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (kScreenW -30)*kscale, 12)];
        [lab setNumberOfLines:0];
        [lab setFont:[UIFont systemFontOfSize:12]];
        for (int i = 0 ; i<textArray.count; i++) {
            lab.frame = CGRectMake(0, 0, (kScreenW -30)*kscale, 0);
            lab.text = [NSString stringWithFormat:@"%@",[textArray objectAtIndex:i]];
            [lab sizeToFit];
            sum = sum +lab.frame.size.height;
        }
        _bgView.frame = CGRectMake(0, 0, kScreenW, (sum+5+3*37) *kscale);
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50*kscale, kScreenW, _bgView.frame.size.height)];
        
        [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _table.dataSource = self;
        _table.delegate = self;
        [_table setScrollEnabled:NO];
        _table.autoresizesSubviews = YES;
        [_table registerClass:[XDLMSanPostFiveTableViewCell class] forCellReuseIdentifier:@"XDLMSanPostFiveTableViewCell"];
        [_bgView addSubview:_table];
        
        UILabel *lineLab = [self createLabelWithFrame:CGRectMake(15, 49*kscale, kScreenW - 30, 0.5) text:@"" textFont:nil textColor:nil];
        lineLab.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1];
        [_bgView addSubview:lineLab];
    }
}

- (void)reloadData:(NSArray *)dic
{
    _titleLab.text = _title;
    textArray = dic;
    [_table reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDLMSanPostFiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDLMSanPostFiveTableViewCell" forIndexPath:indexPath];
    if (textArray) {
        [cell setTitle:[self.titleArray objectAtIndex:indexPath.row] text:[NSString stringWithFormat:@"%@",[textArray objectAtIndex:indexPath.row]]];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (kScreenW -30)*kscale, 12)];
    [lab setNumberOfLines:0];
    [lab setFont:[UIFont systemFontOfSize:12]];
    //[lab setText:[textArray objectAtIndex:indexPath.row]];
    lab.text = [NSString stringWithFormat:@"%@",[textArray objectAtIndex:indexPath.row]];
    [lab sizeToFit];
    return (34+lab.frame.size.height)*kscale;
}

-(void)btnCilck:(UIButton *)sender
{
    _btn.selected = !_btn.selected;
    if (sender.selected) {
        //展开的布局
        _btn.frame = CGRectMake(kScreenW - 15- 12*kscale, 17*kscale, 10, 6);
        [_btn setBackgroundImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateSelected];
        [self setUI];
        if (self.isSelect) {
            self.isSelect(YES);
        }
        [_bgView setHidden:NO];
        
        
    }
    else
    {
                if (self.isSelect) {
            self.isSelect(NO);
        }
        _btn.frame = CGRectMake(kScreenW - 15- 9*kscale, 17*kscale, 6, 10);
        [_btn setBackgroundImage:[UIImage imageNamed:@"rightcell"] forState:UIControlStateNormal];

        [_bgView setHidden:YES];
    }
    
    
    
}
//创建lab
- (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text textFont:(UIFont *)font textColor:(UIColor *)color {
    
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = color;
    label.font = font;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
    
}
//改变行间距
- (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}
//改变字间距
- (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}
- (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}
- (NSArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
